package com.itheima.health.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author kcs
 * @date 2021/11/30
 * @TIME:23:21
 */
@Controller
//@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login.do")
    public String login(String username,String password){

        //1.数据校验
        if(null == username || null == password || username == "" || password == ""){
            //return new Result(false,"用户名或者密码为空","redirect:/login.html");
            return "redirect:/login.html";
        }

        //2.获取密码进行加密

        //3.校验用户名密码是否正确
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getUsername,username);
        wrapper.eq(User::getPassword,password);
        User user = userService.getOne(wrapper);
        if(user == null){
            return "redirect:/login.html";//用户名或者密码错误
        }

        //return new Result(true,"登录成功");
        return "redirect:/pages/main.html";
    }

}
